package com.dnake.utils;

import android.util.Log;

import com.dnake.Constant;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCmdMessageBody;
import com.hyphenate.chat.EMMessage;
import com.orhanobut.logger.Logger;
import com.wx.android.common.util.SharedPreferencesUtils;

/**
 * @author haoyuliu
 * @since 2017/7/13
 */

public class EasemobUtils {

    /**
     * 登录环信
     * @param account
     * @param password
     */
    public static void loginEasemob(final String account, final String password){
        EMClient.getInstance().logout(true, new EMCallBack() {
            @Override
            public void onSuccess() {
                EMClient.getInstance().login(account,password,new EMCallBack() {//回调
                    @Override
                    public void onSuccess() {
                        Log.d("main", "登录聊天服务器成功！");
                    }

                    @Override
                    public void onProgress(int progress, String status) {

                    }

                    @Override
                    public void onError(int code, String message) {
                        Log.d("main", "登录聊天服务器失败！");
                    }
                });
            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }
    /**
     * 发送透传消息
     * @param action
     * @param name
     */
    public static void sendCMDmessage(String action, String name){
        Logger.e(action);
        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
        EMCmdMessageBody cmdBody = new EMCmdMessageBody(action);
        cmdMsg.setAttribute("Cell_Information", SharedPreferencesUtils.getString(Constant.SP_COMMUNITYNAME)
                +" "+ SharedPreferencesUtils.getString(Constant.SP_COMBINNAME));
        cmdMsg.setTo(name);
        cmdMsg.addBody(cmdBody);
        EMClient.getInstance().chatManager().sendMessage(cmdMsg);

    }
    /**
     * 发送图片消息
     * @param imagePath
     * @param username
     * @param entranceId
     * @param roomNo
     */
    public static void sendImageMessage(String imagePath, String username, String entranceId, String roomNo) {
        EMMessage message = EMMessage.createImageSendMessage(imagePath, false, username);
        Logger.e(username);
        if (message == null) {
            return;
        }
        message.setAttribute("em_force_notification", true);
        message.setAttribute("entranceId", entranceId);
        message.setAttribute("Cell_Information", roomNo);
        //send message
        EMClient.getInstance().chatManager().sendMessage(message);
    }


}
